#include <json/json.h>
#include "UI_Logic_Interface.h"
#include <include/sdk/misc.h>

using namespace std;

//UI_Logic_Interface* UI_Logic_Interface::instance = nullptr;

//UI_Logic_Interface* UI_Logic_Interface::instance = nullptr;
UI_Logic_Interface::UI_Logic_Interface()
{
    /*logicOperations = new Commands;
    playingOperation = new Commands;*/
}

UI_Logic_Interface* UI_Logic_Interface::getInstance() {
    static UI_Logic_Interface instance;
    return &instance;
}

void UI_Logic_Interface::addActMaker(const std::string& filename,int playerId)//添加需要执行的dll
{
    logicExecutor->act[playerId - 1] = make_shared<AiActMaker>(filename.c_str());
}

void UI_Logic_Interface::init() {
    logicExecutor = misc::make_unique<LogicExecutor>();
}

void UI_Logic_Interface::run() {
    logicExecutor->oneRoundRun();
}

int UI_Logic_Interface::checkWin() {
    return logicExecutor->game->getWinner();
}

void UI_Logic_Interface::invokeAI() {
    logicExecutor->fetchCommands(1);
    logicExecutor->fetchCommands(2);
}

void UI_Logic_Interface::sendCommand(Commands pCommand) {
    logicExecutor->sendCommand(pCommand);
}

std::string UI_Logic_Interface::getReplay() {
    return jsonToString(logicExecutor->game->getReplay());
}

void UI_Logic_Interface::pushInfo(const std::string &info, int playerId) {
    logicExecutor->pushInfo(playerId, info);
}

std::string UI_Logic_Interface::getReplayOneturn() {
    int turn = logicExecutor->game->nowRound;
    return jsonToString(logicExecutor->game->replay_data[turn - 1]);
}

Commands UI_Logic_Interface::getPlayerCommand(int playerId){
    Commands p1 = logicExecutor->main_cmd, p2;
    for (auto i: p1.cmds) if (i.name == "UseSkill") {
            if (logicExecutor->game->entityManager.findEntityByID(i.params[0])->properties["team"]
                == to_string(playerId)) {
                p2.cmds.push_back(i);
            }
        }
    return p2;
}

void UI_Logic_Interface::addFeedbackActMaker(const std::string &filename, int playerId) {
    logicExecutor->feedback[playerId - 1] = make_shared<FeedbackActMaker>(filename.c_str());
}

void UI_Logic_Interface::pushInfoFeedback(const std::string &info, int playerId) {
    logicExecutor->pushInfoFeedback(playerId, info);
}

void UI_Logic_Interface::invokeFeedback() {
    logicExecutor->invokeFeedback(1);
    logicExecutor->invokeFeedback(2);
}

std::string UI_Logic_Interface::fetchShowinfo() {
    return logicExecutor->fetchShowInfo();
}

int UI_Logic_Interface::checkWinnerFinal() {
    return logicExecutor->game->getWinnerFinal();
}

